﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 域字典服务
    /// </summary>
    public class StdDomainDictService : CrudAppService<StdDomainDict, StdDomainDictDto, Guid, StdDomainDictInput, StdDomainDictCreateDto, StdDomainDictUpdateDto>
    {
        private IRepository<StdDomainDict, Guid> _StdDomainDictRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="StdDomainDictRepository"></param>
        public StdDomainDictService(IRepository<StdDomainDict, Guid> StdDomainDictRepository) : base(StdDomainDictRepository)
        {
            _StdDomainDictRepository = StdDomainDictRepository;
        }

        /// <summary>
        /// 获取域字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<StdDomainDictDto>> GetListAsync(StdDomainDictInput input)
        {
            var iQuery = await _StdDomainDictRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), i => i.DomainCodg.Contains(input.Filter) || i.DomainName.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<StdDomainDict>, List<StdDomainDictDto>>(iQuery.ToList());

                return new PagedResultDto<StdDomainDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<StdDomainDict>, List<StdDomainDictDto>>(items.ToList());

                return new PagedResultDto<StdDomainDictDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 批量上传
        /// </summary>
        /// <returns></returns>
        public async Task PostBatchAsync(List<StdDomainDictCreateDto> input)
        {
            var StdDomainDicts = ObjectMapper.Map<List<StdDomainDictCreateDto>, List<StdDomainDict>>(input);
            await _StdDomainDictRepository.InsertManyAsync(StdDomainDicts);
        }
    }
}
